(ns server.server
  (:gen-class)
  (:use server.dbm 
	server.server
	clojure.test)
   (:import java.io.RandomAccessFile))

(def dbase1 "server/test-dbase-1.db")
(def dbase2 "server/test-dbase-2.db")

(defn offset-record
  "Gets a record from a specifical record number"
  [file-name nrecord]
  (with-open [file (RandomAccessFile. file-name "r")]
    (let [magic-number (.readInt file)
	  offset       (.readInt file)
	  num-fields   (.readShort file)
	  fields-meta  (read-fields-meta file num-fields)
	  RecNo (- nrecord 1)]
      (set-position-record file file-name RecNo)
      (read-one-record file fields-meta))))

(def result1 [7 ["name" "location" "size" "smoking" "rate" "date" "owner"] 29]
)
(def result2 [{:owner "", :date "2005/03/23", :rate "$210.00", :smoking "N", :size "2", :location "Smallville", :name "Excelsior", :deleted false} {:owner "", :date "2004/11/15", :rate "$150.00", :smoking "Y", :size "4", :location "Smallville", :name "Splendide", :deleted false} {:owner "", :date "2003/02/08", :rate "$170.00", :smoking "N", :size "4", :location "Whoville", :name "Excelsior", :deleted false} {:owner "", :date "2003/11/24", :rate "$180.00", :smoking "Y", :size "2", :location "Whoville", :name "Splendide", :deleted false} {:owner "", :date "2004/06/14", :rate "$160.00", :smoking "N", :size "4", :location "Whoville", :name "Pandemonium", :deleted false} {:owner "", :date "2003/07/26", :rate "$80.00", :smoking "N", :size "6", :location "Metropolis", :name "Dew Drop Inn", :deleted false} {:owner "", :date "2005/03/10", :rate "$240.00", :smoking "Y", :size "2", :location "Metropolis", :name "Splendide", :deleted false} {:owner "", :date "2005/02/01", :rate "$200.00", :smoking "Y", :size "2", :location "Pleasantville", :name "Splendide", :deleted false} {:owner "", :date "2004/11/04", :rate "$180.00", :smoking "N", :size "6", :location "Pleasantville", :name "Palace", :deleted false} {:owner "", :date "2003/11/21", :rate "$80.00", :smoking "Y", :size "2", :location "Pleasantville", :name "Excelsior", :deleted false} {:owner "", :date "2004/03/17", :rate "$240.00", :smoking "N", :size "4", :location "Digitopolis", :name "Splendide", :deleted false} {:owner "", :date "2005/09/16", :rate "$130.00", :smoking "N", :size "6", :location "Digitopolis", :name "Palace", :deleted false} {:owner "", :date "2003/06/27", :rate "$80.00", :smoking "N", :size "4", :location "Atlantis", :name "Grandview", :deleted false} {:owner "", :date "2003/10/05", :rate "$190.00", :smoking "N", :size "4", :location "Atlantis", :name "Bed & Breakfast & Business", :deleted false} {:owner "", :date "2005/08/19", :rate "$210.00", :smoking "Y", :size "6", :location "EmeraldCity", :name "Pandemonium", :deleted false} {:owner "", :date "2003/02/08", :rate "$100.00", :smoking "N", :size "6", :location "EmeraldCity", :name "Grandview", :deleted false} {:owner "", :date "2003/10/16", :rate "$180.00", :smoking "Y", :size "6", :location "EmeraldCity", :name "Splendide", :deleted false} {:owner "", :date "2003/05/17", :rate "$150.00", :smoking "Y", :size "6", :location "Bali Hai", :name "Excelsior", :deleted false} {:owner "", :date "2005/02/11", :rate "$140.00", :smoking "N", :size "6", :location "Bali Hai", :name "Bed & Breakfast & Business", :deleted false} {:owner "", :date "2003/01/19", :rate "$200.00", :smoking "Y", :size "4", :location "Xanadu", :name "Dew Drop Inn", :deleted false} {:owner "", :date "2004/11/07", :rate "$230.00", :smoking "N", :size "4", :location "Xanadu", :name "Castle", :deleted false} {:owner "", :date "2004/09/07", :rate "$220.00", :smoking "N", :size "4", :location "Paravel", :name "Excelsior", :deleted false} {:owner "", :date "2005/11/06", :rate "$110.00", :smoking "Y", :size "4", :location "Paravel", :name "Castle", :deleted false} {:owner "", :date "2004/12/17", :rate "$90.00", :smoking "N", :size "4", :location "Hobbiton", :name "Castle", :deleted false} {:owner "", :date "2004/12/23", :rate "$140.00", :smoking "N", :size "6", :location "Hobbiton", :name "Splendide", :deleted false} {:owner "", :date "2004/04/12", :rate "$80.00", :smoking "N", :size "6", :location "Hobbiton", :name "Excelsior", :deleted false} {:owner "", :date "2005/04/23", :rate "$110.00", :smoking "N", :size "4", :location "Lendmarch", :name "Grandview", :deleted false} {:owner "", :date "2003/05/19", :rate "$250.00", :smoking "Y", :size "4", :location "Lendmarch", :name "Splendide", :deleted false} {:owner "", :date "2003/04/20", :rate "$120.00", :smoking "Y", :size "4", :location "Lendmarch", :name "Palace", :deleted false}]
)

(def testcreate {:owner "", :date "2010/04/26", :rate "$250.00", :smoking "Y", :size "4", :location "TestLand", :name "Test"})

(def testupdate {:owner "", :date "2010/04/26", :rate "$250.00", :smoking "Y", :size "4", :location "TestLand", :name "Test2"})

(def result3 {:owner "", :date "2010/04/26", :rate "$250.00", :smoking "Y", :size "4", :location "TestLand", :name "Test", :deleted false})

(def result4 {:owner "", :date "2010/04/26", :rate "$250.00", :smoking "Y", :size "4", :location "TestLand", :name "Test2", :deleted false}) 

(def result5 {:owner "", :date "2010/04/26", :rate "$250.00", :smoking "Y", :size "4", :location "TestLand", :name "Test2", :deleted true})

(def result6 "c4ab583eb01e955a601f5570103a7124") 



(deftest database-manager-test
  (is (= (get-record-size dbase1) 161))
  (is (= (records-number dbase1) 29))
  (is (= (get-records-offset dbase1) 74))
  (is (= (read-records dbase1) result2))
  (is (= (read-records-not-deleted dbase1) result2))
  (is (= (get-deleted-record dbase1) -1))
  (is (not(= (get-deleted-record dbase2) -1))))

(deftest server-test
  (is (= (describe "server/test-dbase-1") result1))
  (is (= (select "server/test-dbase-1") result2))
  (is (= (create "server/test-dbase-2" testcreate) 1))
  (is (= (offset-record dbase2 29) result3))
  (is (= (update "server/test-dbase-2" 29 testupdate ) 1))
  (is (= (offset-record dbase2 29) result4))
  (is (= (delete "server/test-dbase-2" 29) 1))
  (is (= (offset-record dbase2 29) result5))
  (is (= (hashDB "server/test-dbase-1") result6))
  (is (= (out-of-date "server/test-dbase-1" result6) "up-to-date"))
  (is (= (out-of-date "server/test-dbase-2" result6) "out-of-date"))
  (is (thrown? java.io.FileNotFoundException (create "test" testcreate)))
  (is (thrown? java.io.FileNotFoundException (describe "test")))
  
  )

(run-tests)

  

